/*
 * RectangleLineLocation.java
 *
 * Created on 19 maart 2008, 10:58
 *
 * @author wijnand.schepens@hogent.be
 */
package model.graphic.locations;

import java.io.Serializable;

/**
 * Adapter Location contraining a location to lie on a horizontal line to the right
 * of a reference location (at a maximal distance given by the x-coordinate of a second reference location)
 */
public class RectangleLineLocation implements Location, Serializable {

    protected Location location;
    protected Location leftLocation;
    protected Location rightLocation;

    public RectangleLineLocation(Location location, Location leftLocation, Location rightLocation) {
        this.location = location;
        this.leftLocation = leftLocation;
        this.rightLocation = rightLocation;
    }

    public int getX() {
        return location.getX();
    }

    public int getY() {
        return location.getY();
    }

    public void setXY(int x, int y) {
        int minX = leftLocation.getX();
        int maxX = rightLocation.getX();
        int minY = leftLocation.getY();
        int maxY = rightLocation.getY();
        int yy;
        int xx;
        if (x > minX && x < maxX) {
            if (Math.abs(y - minY) < Math.abs(maxY - y)) {
                yy = minY;
            } else {
                yy = maxY;
            }
            location.setXY(x, yy);
        } else if (y > minY && y < maxY) {
            if (Math.abs(x - minX) < Math.abs(maxX - x)) {
                xx = minX;
            } else {
                xx = maxX;
            }
            location.setXY(xx, y);
        } else {
            location.setXY(x < minX ? minX : (x > maxX ? maxX : x), y < minY ? minY : (y > maxY ? maxY : y));
        }
    }

    /**
     * to be called when reference locations change
     */
    public void update() {
        this.setXY(this.getX(), this.getY());
    }

    public void print(){
    }
}
